package com.snail.snailvr.network;




import com.alibaba.fastjson.JSON;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.snail.snailvr.utils.ExtendJsonUtil;
import com.snail.snailvr.utils.LogUtils;

import java.io.UnsupportedEncodingException;



/**
 * Created by xixh on 2015/6/16.
 */
public class FSRequest<T> extends Request<T> {
    /** Default charset for FSRequest. */
    protected static final String PROTOCOL_CHARSET = "utf-8";

    private final Response.Listener<T> mListener;
    private final Class<T> clazz;
    private boolean extendJson = true;
    private boolean isHomeJson = true;

    /** The default socket timeout in milliseconds */
    public static final int DEFAULT_TIMEOUT_MS = 10000;

    /** The default number of retries */
    public static final int DEFAULT_MAX_RETRIES_GET = 2;
    public static final int DEFAULT_MAX_RETRIES_POST = 0;

    /** The default backoff multiplier */
    public static final float DEFAULT_BACKOFF_MULT = 1f;


    public FSRequest(int method, String url, String tag, final Class<T> clazz, Response.Listener<T> listener
            , Response.ErrorListener errorListener, boolean extendJson,boolean isHomeJson) {
        super(method, url, errorListener);
        this.isHomeJson=isHomeJson;
        mListener = listener;
        setTag(tag);
        this.clazz = clazz;
        this.extendJson = extendJson;
        if (method == Method.GET) {
            setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES_GET, DEFAULT_BACKOFF_MULT));
        } else if (method == Method.POST) {
            setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES_POST, DEFAULT_BACKOFF_MULT));
        }
    }


    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        try {
            LogUtils.d(this.getUrl() + " result --> " + new String(response.data, PROTOCOL_CHARSET));
            T t;
            if(extendJson) {
                if(isHomeJson){
                    t = ExtendJsonUtil.parseHomeJsonToModel(new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)), clazz);
                }else {
                    t = ExtendJsonUtil.parseJsonToModel(new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)), clazz);
                }
            } else {
                t = JSON.parseObject(response.data, clazz);
            }

            return Response.success(t, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(T response) {
        if (mListener != null)
            mListener.onResponse(response);
    }
}
